//
//  Fraction.m
//  02-ClassAndObject
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "Fraction.h"


@implementation Fraction


- (instancetype)initWithNum:(NSInteger)num Deno:(NSInteger)deno{
    
    self = [super init];
    if (self) {
        self.numerator = num;
        self.denominator = deno;
    }
    return self;
}

+ (instancetype)fracWithNum:(NSInteger)num Deno:(NSInteger)deno{
    
    return [[[self alloc]initWithNum:num Deno:deno]autorelease];
}

- (Fraction *)addFraction:(Fraction *)anotherFraction{
    NSInteger newNum = self.numerator * anotherFraction.denominator + self.denominator * anotherFraction.numerator;
    NSInteger newDen = self.denominator * anotherFraction.denominator;
    Fraction *addFraction = [[Fraction alloc] initWithNum:newNum Deno:newDen];
    [addFraction reduce];
    [addFraction outPut];
    return addFraction;
}

- (Fraction *)subFraction:(Fraction *)anotherFraction{
    NSInteger newNum = self.numerator * anotherFraction.denominator - self.denominator * anotherFraction.numerator;
    NSInteger newDen = self.denominator * anotherFraction.denominator;
    Fraction *subFraction = [[Fraction alloc] initWithNum:newNum Deno:newDen];
    [subFraction reduce];
    [subFraction outPut];
    return subFraction;
}
- (Fraction *)mulFraction:(Fraction *)anotherFraction{
    NSInteger newNum = self.numerator * anotherFraction.numerator;
    NSInteger newDen = self.denominator * anotherFraction.denominator;
    Fraction *mulFraction = [[Fraction alloc] initWithNum:newNum Deno:newDen];
    [mulFraction reduce];
    [mulFraction outPut];
    return mulFraction;
}

- (Fraction *)divFraction:(Fraction *)anotherFraction{
    NSInteger newNum = self.numerator * anotherFraction.denominator ;
    NSInteger newDen = self.denominator * anotherFraction.numerator;
    Fraction *divFraction = [[Fraction alloc] initWithNum:newNum Deno:newDen];
    [divFraction reduce];
    [divFraction outPut];
    return divFraction;
    
}

- (NSInteger)maxGYSwithNum1:(NSInteger)num1 Num2:(NSInteger)num2{
    NSInteger min = num1 < num2 ? num1 : num2;
    NSInteger maxGYS = 1;
    int result = abs((int) min);
    
    for (NSInteger i = result; i > 0; i--) {
        if (num1 % i  == 0 && num2 % i == 0) {
            maxGYS = i;
            break;
        }
    }return maxGYS;
}
- (void)reduce {
    NSInteger max = [self maxGYSwithNum1:self.numerator Num2:self.denominator];
    self.numerator /= max;
    self.denominator /= max;
}
- (void)outPut {
    if (self.numerator == self.denominator && self.denominator != 0) {
        NSLog(@"1");
    } else if (self.numerator + self.denominator == 0) {
        NSLog(@"-1");
    } else if (self.denominator == 0) {
        NSLog(@"error");
    } else {
        NSLog(@"%ld / %ld", self.numerator, self.denominator);
    }
    
}












@end
